//
//  ChangeCodeViewController.m
//  DeepBreathing
//
//  Created by rimi on 15/12/17.
//  Copyright © 2015年 rimi. All rights reserved.
//

#import "ChangeCodeViewController.h"

#import "NetworkManger+User.h"

#import "ForgetCodeViewController.h"

@interface ChangeCodeViewController ()

@end

@implementation ChangeCodeViewController
-(void)viewDidLoad{
    
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.93];
    
    self.title = @"修改密码";
    
    
    
    UIImage *image = [UIImage imageNamed:@"返回"];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    
    button.frame = CGRectMake(0, 0, 40, 40);
    
    [button setImage:image forState:UIControlStateNormal];
    
    [button addTarget:self action:@selector(dimiss:) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithCustomView:button];
    
    self.navigationItem.leftBarButtonItem = item;
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:@{@"houlder":@[@"请输入密码",@"请再次输入密码"],@"title":@[@"新密码",@"确认密码"]}];
    
    for (NSInteger index = 0; index < 2; index ++) {
        UITextField *registerPhoneNumber = [[UITextField alloc]init];
        
        registerPhoneNumber.frame = CGRectMake(100, CGRectGetMaxY(self.view.frame)/12 + 40+ index * 50 , 240, 40);
        
        registerPhoneNumber.placeholder = dic[@"houlder"][index];
        
        registerPhoneNumber.backgroundColor = [UIColor whiteColor];
        
        registerPhoneNumber.tag = 300 + index;
        
        registerPhoneNumber.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 40)];
        registerPhoneNumber.leftViewMode = UITextFieldViewModeAlways;
        
        [registerPhoneNumber.layer setBorderWidth:2];
        [registerPhoneNumber.layer setBorderColor:[UIColor colorWithRed:1.0 green:0.8 blue:0.4 alpha:0.84].CGColor];
        
        [self.view addSubview:registerPhoneNumber];
        
        
    }
    
    for (NSInteger index = 0; index < 2; index ++) {
        
        UILabel *label  = [[UILabel alloc]initWithFrame:CGRectMake(30, CGRectGetMaxY(self.view.frame)/12  + 40 + index * 50, 70, 50)];
        
        label.numberOfLines = 2;
        
        label.text = dic[@"title"][index];
        
        label.font = [UIFont systemFontOfSize:16];
        
        [self.view addSubview:label];
        
    }
    
    UIButton *finshbutton = [UIButton buttonWithType:UIButtonTypeSystem];
    
    finshbutton.frame = CGRectMake(30, CGRectGetMaxY(self.view.frame) - 140, 315, 40);
    [finshbutton setTitle:@"完成修改" forState:UIControlStateNormal];
    finshbutton.backgroundColor = [UIColor colorWithRed:1.0 green:0.502 blue:0.0 alpha:0.92];
    
    finshbutton.tintColor = [UIColor whiteColor];
    
    [finshbutton.layer setBorderWidth:1];
    
    [finshbutton.layer setBorderColor:[UIColor blackColor].CGColor];
    
    [finshbutton addTarget:self action:@selector(getsecuritycode2:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:finshbutton];



}
-(void)dimiss:(UIButton *)sender{
    
    [self.navigationController popViewControllerAnimated:YES];
    
}
-(void)getsecuritycode2:(UIButton *)sender{
    
    UITextField *Number = (UITextField *)[self.view viewWithTag:300];
    UITextField *sureNumber = (UITextField *)[self.view viewWithTag:301];
    
    if (Number.text.length < 6 || Number.text.length > 16) {
        
        [self otherpopuppromptBox:@"请输入6 - 16 位的密码"];
        
    }else if(![Number.text isEqualToString:sureNumber.text]){
        
        [self otherpopuppromptBox:@"两次密码不相符"];
    }else if ([Number.text isEqualToString:sureNumber.text]&& Number.text.length > 6 && Number.text.length < 16){
        NSString *string = self.block();
     
     
        [NetworkManger getCodeWhenGetSecurityCodeWithnewPwd:Number.text reNewPwd:sureNumber .text phoneNumber:string successHander:^(id responseObject) {
            [self otherpopuppromptBox:@"修改成功"];
            [self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
            
        } failureHander:^(NSError *error) {
            [self otherpopuppromptBox:@"修改失败"];
        }];
        
        
    }
    
    
    
    
}

#pragma mark -- worning
-(void)otherpopuppromptBox:(NSString *)string{
    
    UIAlertController  *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:string preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    [alert addAction:action];
    [self presentViewController:alert animated:YES completion:nil];
    
    
}

@end
